package cn.itcast.hotel;

import cn.itcast.hotel.constants.HotelConstants;
import org.apache.http.HttpHost;


import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * @author oldwine
 * @date 2022/6/16
 * @Version 1.0
 */
public class EsIndexDemo {

    /**
     * 创建索引库
     *
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        //1.准备Es的客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://122.112.171.22:9200")
                ));
        //2.封装创建索引库的请求对象                             索引库名称
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        //              整个请求参数的json字符串              指定参数类型为json
        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        //3.调用客户端执行请求  indices() 针对索引库操作的,   不带indices() 是针对文档操作的
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //4.解析响应结果
        System.out.println("索引库创建结果" + response.isAcknowledged());
    }

    /**
     * 判断索引库是否存在
     */
    @Test
    public void existIndex() throws IOException {
        //1.创建请求对象
        GetIndexRequest request = new GetIndexRequest("hotel");
        //2.哭客户端调用方法发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        //3.解析响应数据
        System.out.println(exists ? "索引库存在" : "索引库不存在");
    }

    /**
     * 删除索引库
     */
    @Test
    public void deleteIndex() throws IOException {
        //1.创建请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        //2.客户端调用方法发送请求
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        //3.解析响应数据
        boolean b = response.isAcknowledged();
        System.out.println(b?"删除成功":"删除失败");
    }

    /**
     * 针对createIndex()优化 测试方法执行前创建Es客户端对象  执行后 客户端对象关闭
     */

    RestHighLevelClient client = null;

    @BeforeEach
    public void init() {
        //创建Es客户端对象
        client = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://122.112.171.22:9200")
                ));
    }

    @AfterEach
    public void close() {
        //关闭Es客户端
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
